'use strict';

define(['jquery','HtmlModeler','DataHandler','nav_toggler','Templator','template','messgerBox'],function($,HtmlModeler,DataHandler,nav_toggler,Templator,template,messageBox){
	var changePage = function(section,file_path,callback){
			$('header .ul-nav>li').removeClass('active');
			$('header .ul-nav>li>a[href="#'+ section + '"]').parent().addClass('active');

			$('header').removeClass('active');
			nav_toggler.changeTag(true);

			if($('.wrap-page>.page#'+ section).html()){

				if(typeof callback === 'function'){
					callback();
				}

				$('.wrap-page>.page').removeClass('active');
				$('.wrap-page>.page#'+ section).addClass('active')

				window.scrollTo(0,0);
			}else{


				$('.wrap-page>.page#'+ section).load('page/' + file_path,function(){

					if(typeof callback === 'function'){
						callback();
					}

					$('.wrap-page>.page').removeClass('active');
					$(this).addClass('active');

					window.scrollTo(0,0);
				});
			}


	};


	return  {
		main:function(argv){
			var templator = new Templator(template);
			changePage('main','index.html',function(){
				DataHandler.get_index_news(function(data){
					var temp = true;
					$('#news-left').html('');
					$('#news-right').html('');
					for(var key in data){
						if(temp){
							$('#news-left').append(templator.create('index_news_temp',data[key]));
							temp = false;
						}else{
							$('#news-right').append(templator.create('index_news_temp',data[key]));
							temp = true;
						}
					}
				});
			});
		},
		news:function(argv){
			changePage('news','news.html',function(){
				//console.log(Templator.article_list({'abstract':"test","title":"hello"}));
				if(argv['mode'] != 'article'){
					$('#article-area').removeClass('active');
					$('#news-list').addClass('active');
					var templator = new Templator(template);

					DataHandler.get_news_list(function(data){
						$('#news-list>ul').html('');

						for(var key in data){
							$('#news-list>ul').append(templator.create('article_list_temp',data[key]));
						}
					});
					

					var newsest_list = DataHandler.get_newsest_list(function(newsest_list){
						$('#newsest-list>ul').html('');

						for(var key in newsest_list){
							$('#newsest-list>ul').append(templator.create('article_newsest_list_temp',newsest_list[key]));
						}
					});
					
				}else{
					$('#news-list').removeClass('active');
					$('#article-area').addClass('active');
					DataHandler.get_article(argv['id'],function(data){
						$('#article-area article').html(data);
					});
				}
			});
		},
		about_us:function(argv){
			changePage('about_us','about_us.html',function(){

				$('#about_us_header li').on('click',function(){
					

					window.location.hash = "about_us?tag=" +  $(this).children('a').attr('alt');
				});

				switch(argv['tag']){
					case 'rules':{
						$('#about_us_header li').removeClass('active');
						$('#about_us_header li a[alt=rules]').parent().addClass('active');
						$('.tag-page').removeClass('active')
						$('#rules').addClass('active')

						argv['article'] = 'ibm_club_rule';
						DataHandler.get_about_us_article(argv,function(data){
							$('#rules').html(data);
						});
						break;
					}
					case 'show':{
						$('#about_us_header li').removeClass('active');
						$('#about_us_header li a[alt=show]').parent().addClass('active');
						$('.tag-page').removeClass('active')
						$('#show').addClass('active')

						argv['article'] = 'struction';
						DataHandler.get_about_us_article(argv,function(data){
							$('#show').html(data);
						});
						break;
					}
					case 'struction':
					default:{
						$('#about_us_header li').removeClass('active');
						$('#about_us_header li a[alt=struction]').parent().addClass('active');
						$('.tag-page').removeClass('active')
						$('#struction').addClass('active')

						argv['article'] = 'struction';
						DataHandler.get_about_us_article(argv,function(data){
							$('#struction').html(data);
						});
					}

				}
			});
		},
		join_us:function(argv){
			changePage('join_us','join_us.html',function(){
				var check = function(options){
					var checkEmail = function(email){
						var reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/

						if(!reg.test(email)){

							messageBox.show('您输入的邮箱有误，请检查后再输入',false);
							return false;
						}
					}

					var checkPhone = function(phone){
						var reg = /^1\d{10}$/

						if(phone.match(reg)){
							
						}else{

							messageBox.show('您输入的手机号码有误，请检查后再输入',false);
							return false;
						}
					}

					var reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/

					if(!reg.test(options['email'])){

						messageBox.show('您输入的邮箱有误，请检查后再输入',false);
						return false;
					}

					var phone_reg = /^1\d{10}$/

					if(!phone_reg.test(options['mobile'])){
						messageBox.show('您输入的手机号码有误，请检查后再输入',false);
						return false;
					}

					if(!options['name']){
						messageBox.show('您输入的姓名为空，请检查后再输入',false);
						return false;
					}

					if(!options['nation']){
						messageBox.show('您输入的民族为空，请检查后再输入',false);
						return false;
					}

					if(!options['hobby']){
						messageBox.show('您输入的爱好为空，请检查后再输入',false);
						return false;
					}

					if(!options['address']){
						messageBox.show('您输入的宿舍为空，请检查后再输入',false);
						return false;
					}

					if(!options['organization']){
						messageBox.show('您是否曾经加入过其他组织，如果没有请填写无，不能为空，请检查后再输入',false);
						return false;
					}

					if( typeof options['technoligy'] === 'undefined'){
						messageBox.show('您输入的技术倾向为空，请检查后再输入',false);
						return false;
					}
					if( typeof options['apartment'] === 'undefined') {
						messageBox.show('您输入的想加入的部门为空，请检查后再输入',false);
						return false;
					}
					if( typeof options['agree_to_allocation'] === 'undefined'){
						messageBox.show('您还没有填写是否服从分配，请检查后再输入',false);
						return false;
					}
					if(!options['await'] ){
						messageBox.show('您输入的俱乐部期待为空，请检查后再输入',false);
						return false;
					}
					if(!options['plan']){
						messageBox.show('您输入的大学规划为空，请检查后再输入',false);
						return false;
					}
					if(!options['assess']){
						messageBox.show('您输入的自我评价为空，请检查后再输入',false);
						return false;
					}
					return true;
				}

				var getValue = function(){
					var options = {};

					options['name'] = $('#join_us #name_input').val();
					options['politics'] = $('#join_us #politics_input').val();
					options['sex'] = $('#join_us #sex_input').val();
					options['nation'] = $('#join_us #nation_input').val();
					options['mobile'] = $('#join_us #mobile_input').val();
					options['email'] = $('#join_us #email_input').val();
					options['hobby'] = $('#join_us #hobby_input').val();
					options['address'] = $('#join_us #address_input').val();
					options['organization'] = $('#join_us #organization_input').val();
					options['technoligy'] = $('#join_us #tech_input input:radio:checked').val();
					options['self_tech'] = $('#join_us #tech_input #apartment input:text').val();
					options['apartment'] = $('#join_us #apartment_input input:radio:checked').val();
					options['agree_to_allocation'] = $('#join_us #apartment_input #agree_to_allocation input:radio:checked').val();
					options['await'] = $('#join_us #await_input').val();
					options['plan'] = $('#join_us #plan_input').val();
					options['assess'] = $('#join_us #assess_input').val();

					return options;
				}

				$('#join_us input[type=radio]').bind('click',function(){
					$(this).parent().children('input:radio').each(function(){
						this.checked = null;
					});
					this.checked = 'checked';
				})

				$('.button-area>#submit-btn').bind('click',function(){
					var options = getValue();
					
					if(check(options)){
						DataHandler.regist(options,function(data){
							var result = JSON.parse(data)
							if(result.status == "1"){
								messageBox.show("恭喜您报名成功！！！",true);
							}else{
								messageBox.show("出错！！！请检查您是否已经注册过",false);
							}
						})


					}
				})

				$('.button-area>#clear-btn').bind('click',function(){
					$('#join_us input').val(" ");
				})
			});
		}
	};

	

});